package com.zjj.controller;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TaskController {

	@Autowired
	private Task task;

	@RequestMapping("/test1")
	public void test1() {
		try {
			task.task1();
			task.task2();
			task.task3();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/test2")
	public void test2() {
		try {
			long start = System.currentTimeMillis();

			Future<String> t1 = task.futureTask1();
			Future<String> t2 = task.futureTask2();
			Future<String> t3 = task.futureTask3();

			while (true) {
				if (t1.isDone() && t2.isDone() && t3.isDone()) {
					// 三个任务都调用完成，退出循环等待
					break;
				}
				Thread.sleep(1000);
			}
			long end = System.currentTimeMillis();

			System.out.println("所有任务完成 --- 总耗时：" + (end - start) + "毫秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
